use crate::editor::ui::status::DocumentStatus;
use crate::editor::{Terminal, ui::UI};
use crate::prelude::*;

#[derive(Default)]
pub struct StatusBar {
    current_status: DocumentStatus,
    needs_redraw: bool,
    size: Size,
}

impl StatusBar {
    pub fn update_status(&mut self, new_status: DocumentStatus) {
        if new_status != self.current_status {
            self.current_status = new_status;
            self.set_needs_redraw(true);
        }
    }
}

impl UI for StatusBar {
    fn set_needs_redraw(&mut self, value: bool) {
        self.needs_redraw = value;
    }

    fn needs_redraw(&self) -> bool {
        self.needs_redraw
    }

    fn set_size(&mut self, size: Size) {
        self.size = size;
    }

    fn draw(&mut self, origin_row: RowIdx) -> Result<(), std::io::Error> {
        // 状态栏显示的最终信息
        let status = self.current_status.to_string(self.size.width);

        Terminal::print_inverted_row(origin_row, &status)?;

        Ok(())
    }
}
